package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.PanelPanelDtoConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IHomeServiceImpl implements IHomeService {
@Autowired
    PanelMapper panelMapper;
@Autowired
    PanelContentMapper panelContentMapper;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse homePageResponse = new HomePageResponse();
        try {
            List<Panel> list = panelMapper.selectAll();
            for (Panel panel: list
                 ) {
                Integer id = panel.getId();
                List<PanelContentItem> panelContentItems1 = panelContentMapper.selectPanelContentAndProductWithPanelId(id);
                panel.setPanelContentItems(panelContentItems1);
            }

            Set<PanelDto> panelDtoList=new HashSet<>();
            for (Panel panel:list
                 ) {
                PanelDto panelDto = PanelPanelDtoConverter.item2Dto(panel);
                panelDtoList.add(panelDto);
            }

            homePageResponse.setPanelContentItemDtos(panelDtoList);
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(homePageResponse, e);

        }

        return homePageResponse;
    }
}
